home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Programming / GMS / GMSDev / Source / C / Blitter / RainingBobs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-15  |  6.2 KB  |  228 lines

  1. /* Dice: 1> dcc -l0 -mD dpk.o RainingBobs.c -o RainingBobs
  2. **
  3. ** This is a demonstration of raining bobs, which I use as a test routine to
  4. ** see how fast some of my blitter routines are.  It's a good example of using
  5. ** MBOB's, try out different MAX_IMAGES values to see how many you can get on
  6. ** screen.  **120** 16 colour 16x8 bobs just manage to run at full speed on my
  7. ** A1200+FAST, change the value if you have a faster machine (600 can be very
  8. ** interesting :-).
  9. **
  10. ** Technical notes
  11. ** ---------------
  12. ** This demo takes direct advantage of some special GMS blitting features,
  13. ** such as restored clearing without masks (gain: 10%), and 16 pixel
  14. ** alignment (gain: 15%).  That allows us to have 25% more Bob's on screen!
  15. **
  16. ** The fact that GMS will use the CPU to draw and clear images when the blitter
  17. ** is busy gives a boost of about 20%+ on an '020, so the overall advantage
  18. ** over a bog standard blitting function (eg BltBitmap()) is at least 40%.
  19. ** Given that such a function would have to be called 120 times with newly
  20. ** calculated parameters each time to draw, and 120 times to do the clears, we
  21. ** are probably looking at least 65% faster... is that good enough?
  22. */
  23.  
  24. #include <proto/dpkernel.h>
  25.  
  26. BYTE *ProgName      = "Raining Bobs";
  27. BYTE *ProgAuthor    = "Paul Manias";
  28. BYTE *ProgDate      = "February 1998";
  29. BYTE *ProgCopyright = "DreamWorld Productions (c) 1996-1998.  Freely distributable.";
  30. BYTE *ProgShort     = "Raining bobs demo.";
  31.  
  32. #define MAX_IMAGES 115
  33.  
  34. struct NBEntry {
  35.   struct MBEntry MB;
  36.   WORD   Speed;
  37.   WORD   Set;
  38.   WORD   FChange;
  39.   WORD   Locked;
  40. };
  41.  
  42. struct GScreen *screen;
  43. struct Restore *restore;
  44. struct MBob    *rain;
  45. struct JoyData *joydata;
  46. struct Picture *bobspic;
  47. struct NBEntry *Images;
  48.  
  49. WORD counter=0;
  50.  
  51. WORD RainFrames[] = {
  52.   0,8*0,  0,8*1,  0,8*2,  0,8*3, /* Red rain */
  53.   8,8*0,  8,8*1,  8,8*2,  8,8*3, /* Green rain */
  54.  16,8*0, 16,8*1, 16,8*2, 16,8*3, /* Blue rain */
  55.   -1,-1
  56. };
  57.  
  58. void Demo(void);
  59. void RegenerateBob(struct MBob *, struct NBEntry *);
  60. void UpdateBob(struct MBob *, struct NBEntry *);
  61.  
  62. /***************************************************************************/
  63.  
  64. void main(void) {
  65.   struct FileName bobfile  = { ID_FILENAME, "GMS:demos/data/PIC.Pulse" };
  66.  
  67.   if (bobspic = InitTags(NULL,
  68.       TAGS_PICTURE, NULL,
  69.         PCA_BitmapTags, NULL,
  70.         BMA_MemType,    MEM_BLIT,
  71.         TAGEND,         NULL,
  72.       PCA_Source,   &bobfile,
  73.       TAGEND)) {
  74.  
  75.    if (screen = InitTags(NULL,
  76.        TAGS_SCREEN, NULL,
  77.        GSA_Attrib,  SCR_DBLBUFFER,
  78.          GSA_BitmapTags, NULL,
  79.          BMA_Palette,    bobspic->Bitmap->Palette,
  80.          TAGEND,         NULL,
  81.        TAGEND)) {
  82.  
  83.     if (restore = InitTags(screen->Bitmap,
  84.         TAGS_RESTORE, NULL,
  85.         RSA_Entries,  MAX_IMAGES,
  86.         RSA_Buffers,  2,
  87.         TAGEND)) {
  88.  
  89.      if (Images = AllocMemBlock(sizeof(struct NBEntry)*MAX_IMAGES,MEM_DATA)) {
  90.  
  91.       if (rain = InitTags(screen->Bitmap,
  92.           TAGS_MBOB,      NULL,
  93.           MBA_AmtEntries, MAX_IMAGES,
  94.           MBA_GfxCoords,  &RainFrames,
  95.           MBA_Width,      8,
  96.           MBA_Height,     8,
  97.           MBA_Attrib,     BBF_GENMASKS|BBF_CLIP|BBF_CLRNOMASK|BBF_CLEAR,
  98.           MBA_Source,     bobspic,
  99.           MBA_EntrySize,  sizeof(struct NBEntry),
  100.           MBA_EntryList,  Images,
  101.           TAGEND)) {
  102.  
  103.        if (joydata = Init(Get(ID_JOYDATA), NULL)) {
  104.           Display(screen);
  105.           Demo();
  106.        }
  107.       }
  108.      }
  109.     }
  110.    }
  111.   Free(joydata);
  112.   Free(rain);
  113.   FreeMemBlock(Images);
  114.   Free(restore);
  115.   Free(screen);
  116.   Free(bobspic);
  117.   }
  118. }
  119.  
  120. /****************************************************************************
  121. ** Keep X coordinates at multiples of 8 for increased speed.
  122. */  
  123.  
  124. void RegenerateBob(struct MBob *bob, struct NBEntry *entry)
  125. {
  126.   entry->MB.YCoord = -8;
  127.   entry->MB.XCoord = (FastRandom(screen->Width) - (bob->Width/2)) & 0xfff8;
  128.   entry->MB.Frame  = FastRandom(12);
  129.   entry->Speed     = FastRandom(8)+2;
  130.   entry->Set       = (entry->MB.Frame)/3;
  131.   entry->FChange   = 1;
  132.   entry->Locked    = ~entry->Locked;
  133. }
  134.  
  135. /***************************************************************************/
  136.  
  137. void UpdateBob(struct MBob *bob, struct NBEntry *entry)
  138. {
  139.    /* Check if bob entry has left the screen */
  140.  
  141.    entry->MB.YCoord += entry->Speed;
  142.  
  143.    if (entry->MB.YCoord >= screen->Height) {
  144.       RegenerateBob(bob,entry);
  145.    }
  146.  
  147.    /* Animation */
  148.  
  149.    if ((entry->Locked) AND ((counter & 0x0003) IS NULL)) {
  150.       if (entry->FChange < 0) {
  151.          /* Negative */
  152.          if (entry->Set IS 1) {         /* Green */
  153.             if ((--entry->MB.Frame) < 4) {
  154.                entry->FChange  = 1;
  155.                entry->MB.Frame = 4;
  156.             }
  157.          }
  158.          else if (entry->Set IS 2) {    /* Blue */
  159.             if ((--entry->MB.Frame) < 8) {
  160.                entry->FChange  = 1; 
  161.                entry->MB.Frame = 8;
  162.             }
  163.          }
  164.          else {                         /* Red */
  165.             if ((--entry->MB.Frame) < 0) {
  166.                entry->FChange  = 1;
  167.                entry->MB.Frame = 0;
  168.             }
  169.          }
  170.       }
  171.       else {
  172.          /* Positive */
  173.          if (entry->Set IS 1) {        /* Green */
  174.             if ((++entry->MB.Frame) > 7) {
  175.                entry->FChange  = -1;
  176.                entry->MB.Frame = 6;
  177.             }
  178.          }
  179.          else if (entry->Set IS 2) {   /* Blue */
  180.             if ((++entry->MB.Frame) > 11) {
  181.                entry->FChange  = -1; 
  182.                entry->MB.Frame = 10;
  183.             }
  184.          }
  185.          else {                        /* Red */
  186.             if ((++entry->MB.Frame) > 3) {
  187.                entry->FChange  = -1;
  188.                entry->MB.Frame = 2;
  189.             }
  190.          }
  191.       }
  192.    }
  193. }
  194.  
  195. /***************************************************************************/
  196.  
  197. void Demo(void)
  198. {
  199.   struct NBEntry *entry;
  200.   WORD i;
  201.  
  202.   /* Generate the bob entries */
  203.  
  204.   entry = (struct NBEntry *)rain->EntryList;
  205.   for (i = rain->AmtEntries; i > 0; i--) {
  206.     RegenerateBob(rain,entry);
  207.     entry->MB.YCoord = FastRandom(screen->Height);
  208.     entry++;
  209.   }
  210.  
  211.   do {
  212.     Query(joydata);
  213.     counter++;
  214.  
  215.     entry = (struct NBEntry *)rain->EntryList;
  216.     for (i = rain->AmtEntries; i > 0; i--) {
  217.        UpdateBob(rain,entry);
  218.        entry++;
  219.     }
  220.  
  221.     Activate(restore);
  222.     DrawBob(rain);
  223.     WaitAVBL();
  224.     SwapBuffers(screen);
  225.   } while (!(joydata->Buttons & JD_LMB));
  226. }
  227.  
  228.